package com.nf.bookstytem.uitl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class QueryRunnerWrapper {
	private static QueryRunner qr=new QueryRunner(DataSourceHelper.getDataSource(),true);
	public static <T>List <T> query(String sql,BeanListHandler<T> rsh){
		List<T>entityList =null;
		try {
			entityList=qr.query(sql,rsh);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("操作错误"+e.getMessage());
		}
		return entityList;
	}
	public static int update(String sql,Object...params) 
	{
		int n=0;
		try {
			n=qr.update(sql,params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("操作错误"+e.getMessage());
		}
		return n;
	}
	
	public static <T> int queryCount(String sql,ScalarHandler<T> scl) {
		int n=0;
		try {
			n=(int) qr.query(sql, scl);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
}
